#include <stdbool.h>
#include <limits.h>
#include <stdio.h>

bool find132pattern(int *nums, int numsSize) {
//    int n = numsSize;
//    int candidate_k[n], top = 0;
//    candidate_k[top++] = nums[n - 1];
//    int max_k = INT_MIN;
//
//    for (int i = n - 2; i >= 0; --i) {
//        if (nums[i] < max_k) {
//            return true;
//        }
//        while (top && nums[i] > candidate_k[top - 1]) {
//            max_k = candidate_k[--top];
//        }
//        if (nums[i] > max_k) {
//            candidate_k[top++] = nums[i];
//        }
//    }
    int n = numsSize;
    int candidate_k[n];
    int top = 0;
    candidate_k[top++] = nums[n - 1];
    int max_k = INT_MAX;
    for (int i = n - 2; i >= 0; i--) {
        if (nums[i] < max_k) {
            return true;
        }
        while (top && nums[i] > candidate_k[top - 1]) {
            max_k = candidate_k[--top];
        }
        if (nums[i] > max_k) {
            candidate_k[top++] = nums[i];
        }
    }
    return false;
}

int main() {
//    [1,2,3,4]
    int test[4] = {1, 2, 3, 4};
    bool ret = find132pattern(test, 4);
    printf("%d", ret);
}

